/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package biz;

import da.ComplainJpaController;
import da.exceptions.PreexistingEntityException;
import da.exceptions.RollbackFailureException;
import entities.Complain;
import java.util.LinkedList;
import java.util.List;
import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.persistence.TypedQuery;
import javax.transaction.UserTransaction;

/**
 *
 * @author THOHCGC00177
 */
@Stateless
@TransactionManagement(TransactionManagementType.BEAN)
public class ComplainProgess {

    @Resource
    private UserTransaction utx;
    @PersistenceUnit(unitName = "ProjectSem4PU")
    private EntityManagerFactory emf = null;
    private ComplainJpaController jpa;

    public ComplainJpaController getJpa() {
        if (jpa == null) {
            jpa = new ComplainJpaController(utx, emf);
        }
        return jpa;
    }
    /**
     * 
     * Function create complain
     * @param complain
     * @throws PreexistingEntityException
     * @throws RollbackFailureException
     * @throws Exception 
     * 
     */
    public void createComplain(Complain complain) throws PreexistingEntityException, RollbackFailureException, Exception {
        getJpa().create(complain);
        
    }
    /**
     * 
     *Function Show all complain
     * @return 
     */
    public List<Complain> getAllComplain() {        
        return getJpa().findComplainEntities();
    }
    /**
     * 
     * Function get complain with Index
     * @return 
     */
    public List<Complain> getComplainForIndex() {
        List<Complain> list = new LinkedList<Complain>();
        List<Complain> lists = getJpa().findComplainEntities();
        int lengt = lists.size();
        for (int i = lengt - 1; i > lengt - 4; i--) {
            Complain item = lists.get(i);
            list.add(item);
        }
        return list;
    }
    /**
     * Function get a complain by complainID
     * @param id
     * @return 
     */
    public Complain getComplainById(int id) {
        String query = "SELECT c FROM Complain c WHERE c.id = :id";
        TypedQuery<Complain> tq = emf.createEntityManager().createQuery(query, Complain.class);
        tq.setParameter("id", id);
        try {
            return tq.getSingleResult();
        } catch (Exception e) {
            return null;
        }
    }
    /**
     * 
     * 
     * Function update Admin Processing (Change complain form No to yes
     * when Admin send a complain to a Technician)
     * 
     * @param complain 
     */
     public void updateProcessing(Complain complain) {        
        try {
            utx.begin();
            Complain complainx = getJpa().findComplain(complain.getId());
            complainx.setProcessing(complain.getProcessing());
            utx.commit();
        } catch (Exception e) {
        }
    }
     /**
      * 
      * 
      *Function update  Status(Change from news to read when technician read the complain
      * and change to complete when technician answer)
      * @param complain 
      */
     public void updateStatus(Complain complain) {
        try {
            utx.begin();
            Complain complainx = getJpa().findComplain(complain.getId());
            complainx.setStatus(complain.getStatus());
            utx.commit();
        } catch (Exception e) {
        }
    }
    
     
}
